package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IOuterWebsiteDao;
import com.magicode.cms.entiy.base.Advertise;
import com.magicode.cms.entiy.base.OuterWebsite;
import com.magicode.cms.service.IOuterWebsiteService;
import com.magicode.common.dao.ICommonDaoService;

@Service("serviceOuterWebsite")
public class OuterWebsiteServiceImpl extends BaseServiceImpl<OuterWebsite,IOuterWebsiteDao> implements IOuterWebsiteService,UtilPath{

	@Autowired
	@Qualifier("daoOuterWebsite")
	private IOuterWebsiteDao outerWebsiteDao;
	public IOuterWebsiteDao getDao() throws Exception {
		return outerWebsiteDao;
	}
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "subTitle");
		map.put(json_title, "子标题");
		map.put("width",200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "url");
		map.put(json_title, "地址");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "创建时间");
		map.put("width", 120);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.OuterWebsite.class.getName()+" t"
				+ " left join t.parseCfg as p "
				+ " where 1 = 1   ";
		if(null!=map && !map.isEmpty()){
			
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
			if(map.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get(field_title)+"%'";
			}
		}
		hql+=" order by t.createTime desc ";
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere); 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("subTitle", "t.subTitle as subTitle");
			gridsel.put("url", "t.url as url");
			gridsel.put("createTime", "t.createTime as createTime");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	
	@Override
	public List<OuterWebsite> selectOuterWebsiteList() {
		Map<String,String> sqlmap = new HashMap<String, String>();
		String sql = "select t from "+OuterWebsite.class.getName() +" t "
				+ " left join t.parseCfg as p  where 1=1 ";
		List<OuterWebsite> list = null;
		try {
			list = this.commonDaoService.queryList(sql, sqlmap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public IOuterWebsiteDao getOuterWebsiteDao() {
		return outerWebsiteDao;
	}
	public void setOuterWebsiteDao(IOuterWebsiteDao outerWebsiteDao) {
		this.outerWebsiteDao = outerWebsiteDao;
	}

}
